package com.oyjd.fw.util;

import java.util.ArrayList;
import java.util.List;

/**
 * android电话本导入文件、导出文件生成
 * 
 * @author 欧建东
 * @email oyjd@163.com
 * 
 */
public class TelBook {
	private static final String encoding = "gb2312";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		// vcfToTxt("D:/00002.vcf", "D:/mmm.txt");

		txtToVcf("D:/mmm.txt", "D:/new.vcf");
	}

	/**
	 * vcf转化为可视txt文件
	 * 
	 * @param vcfPath
	 *            vcf文件路径
	 * @param txtPath
	 *            txt文件路径
	 * @throws Exception
	 */
	public static void vcfToTxt(String vcfPath, String txtPath) throws Exception {
		// 读入
		String c = FileOP.readFile(vcfPath, encoding);
		// 解析
		List<OneCard> list = new ArrayList<OneCard>();
		String[] cs = c.split("END:VCARD\r\n");
		for (String s : cs) {
			if ("".equals(s.trim())) {
				continue;
			}
			// 处理一个节点
			String[] ss = s.split("\r\n");
			OneCard oc = new OneCard();
			for (String o : ss) {
				if (o.startsWith("FN:")) {
					oc.setName(o.substring(3));
				} else if (o.startsWith("FN;")) {
					String n = o.substring(o.lastIndexOf(":") + 1);
					n = n.replaceAll("=", "");
					byte[] b = ByteUtil.bcd2ToByte(n);
					oc.setName(new String(b, "utf-8"));
				} else if (o.startsWith("TEL;")) {
					String ts = o.substring(o.lastIndexOf(":") + 1);
					ts = ts.replaceAll("-", "");
					oc.getPhoneList().add(ts);
				}
			}
			list.add(oc);
		}
		// 合成
		StringBuffer sb = new StringBuffer();
		for (OneCard oc : list) {
			sb.append(oc.getName()).append("=");
			for (int i = 0; i < oc.getPhoneList().size(); i++) {
				if (0 != i) {
					sb.append(",");
				}
				sb.append(oc.getPhoneList().get(i));
			}
			sb.append("\r\n");
		}
		// 输出
		FileOP.writeFile(txtPath, sb.toString(), encoding);
	}

	/**
	 * txt转化为可视vcf文件
	 * 
	 * @param txtPath
	 *            txt文件路径
	 * @param vcfPath
	 *            vcf文件路径
	 * @throws Exception
	 */
	public static void txtToVcf(String txtPath, String vcfPath) throws Exception {
		// 读入
		String c = FileOP.readFile(txtPath, encoding);
		// 解析
		List<OneCard> list = new ArrayList<OneCard>();
		String[] cs = c.split("\r\n");
		for (String s : cs) {
			if ("".equals(s.trim())) {
				continue;
			}
			// 处理一个节点
			String[] ss = s.split("=");
			OneCard oc = new OneCard();
			oc.setName(ss[0]);
			String[] ps = ss[1].split(",");
			for (String p : ps) {
				oc.getPhoneList().add(p);
			}
			list.add(oc);
		}
		// 合成
		StringBuffer sb = new StringBuffer();
		for (OneCard oc : list) {
			sb.append("BEGIN:VCARD\r\n");
			sb.append("VERSION:2.1\r\n");
			int olen = oc.getName().length();
			int blen = oc.getName().getBytes(encoding).length;
			if (olen == blen) {// 英文数字字符串
				sb.append("N:;").append(oc.getName()).append(";;;\r\n");
				sb.append("FN:").append(oc.getName()).append("\r\n");
			} else {
				// 名[欧阳->欧;阳] [欧建东->欧;东;建] [欧阳建东->欧阳;东;建]
				sb.append("N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:");
				String name = oc.getName();
				StringBuffer sb1 = new StringBuffer();
				if (name.length() == 1) {
					sb1.append(getCode(name)).append(";;;");
				} else if (name.length() == 2) {
					sb1.append(getCode(name.substring(0, 1))).append(";");
					sb1.append(getCode(name.substring(1, 2))).append(";;");
				} else if (name.length() >= 3) {
					sb1.append(getCode(name.substring(0, name.length() - 2))).append(";");
					sb1.append(getCode(name.substring(name.length() - 1))).append(";");
					sb1.append(getCode(name.substring(name.length() - 2, name.length() - 1))).append(";");
				}
				sb.append(sb1).append(";\r\n");
				// 全名
				sb.append("FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:").append(getCode(name)).append("\r\n");
			}
			for (String p : oc.getPhoneList()) {
				sb.append("TEL;CELL:").append(p).append("\r\n");
			}
			sb.append("END:VCARD\r\n");
		}
		// 输出
		FileOP.writeFile(vcfPath, sb.toString(), encoding);
	}

	/**
	 * 将中文转成编码
	 * 
	 * @param s
	 * @return
	 * @throws Exception
	 */
	private static String getCode(String s) throws Exception {
		byte[] b = s.getBytes("utf-8");
		String fnt = ByteUtil.byteToStr(b);
		fnt = fnt.replaceAll(" ", "=");
		return "=" + fnt;
	}
}

/**
 * 
 * 个人名片
 * 
 */
class OneCard {
	/**
	 * 姓名
	 */
	private String name;
	/**
	 * 电话号码组
	 */
	private List<String> phoneList = new ArrayList<String>();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<String> getPhoneList() {
		return phoneList;
	}

	public void setPhoneList(List<String> phoneList) {
		this.phoneList = phoneList;
	}
}
